package com.snap.vseries.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.utils.ToastUtils;

import java.text.DecimalFormat;

public class BaselineDialog extends DialogFragment {

    private DecimalFormat decimalFormat = new DecimalFormat("#.#");
    private OnChangedListener listener;
    private TextView num0;
    private TextView num1;
    private TextView num2;
    private TextView num3;
    private TextView num4;
    private TextView num5;
    private TextView num6;
    private TextView num7;
    private TextView num8;
    private TextView num9;
    private TextView startTv;
    private TextView endTv;
    private ImageView clear1;
    private ImageView clear2;
    private ImageView back;
    private ImageView confirm;
    private View startCursor;
    private View endCursor;
    private View startLl;
    private View endLl;
    private int startCycle;
    private int endCycle;
    private int totalCycle;
    private boolean tempFocus = true;

    public BaselineDialog setParam(int totalCycle, int startCycle, int endCycle, OnChangedListener listener) {
        this.startCycle = startCycle;
        this.endCycle = endCycle;
        this.totalCycle = totalCycle;
        this.listener = listener;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.DimDialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_basline_input, container, false);
        num0 = view.findViewById(R.id.num0);
        num1 = view.findViewById(R.id.num1);
        num2 = view.findViewById(R.id.num2);
        num3 = view.findViewById(R.id.num3);
        num4 = view.findViewById(R.id.num4);
        num5 = view.findViewById(R.id.num5);
        num6 = view.findViewById(R.id.num6);
        num7 = view.findViewById(R.id.num7);
        num8 = view.findViewById(R.id.num8);
        num9 = view.findViewById(R.id.num9);


        startTv = view.findViewById(R.id.temp);
        startLl = view.findViewById(R.id.temp_ll);
        startCursor = view.findViewById(R.id.temp_cursor);
        endTv = view.findViewById(R.id.time);
        endLl = view.findViewById(R.id.time_ll);
        endCursor = view.findViewById(R.id.time_cursor);
        clear1 = view.findViewById(R.id.clear1);
        clear2 = view.findViewById(R.id.clear2);
        back = view.findViewById(R.id.back);
        confirm = view.findViewById(R.id.confirm);

        startTv.setText(decimalFormat.format(startCycle));
        endTv.setText(decimalFormat.format(endCycle));

        startCursor.setVisibility(View.VISIBLE);
        endCursor.setVisibility(View.GONE);

        startLl.setOnClickListener(v -> {
            tempFocus = true;
            startCursor.setVisibility(View.VISIBLE);
            endCursor.setVisibility(View.GONE);
        });
        endLl.setOnClickListener(v -> {
            tempFocus = false;
            startCursor.setVisibility(View.GONE);
            endCursor.setVisibility(View.VISIBLE);
        });


        View.OnClickListener numListener = v -> {
            String cur;
            if (tempFocus) {
                cur = startTv.getText().toString().trim();
                if (cur.contains(".")) {
                    String[] sp = cur.split("\\.");
                    if (sp.length > 1 && sp[1].length() >= 1) {
                        return;
                    }
                }
            } else {
                cur = endTv.getText().toString().trim();
            }
            if (cur.equals("0")) {
                cur = "";
            }
            switch (v.getId()) {
                case R.id.num0:
                    cur += "0";
                    break;
                case R.id.num1:
                    cur += "1";
                    break;
                case R.id.num2:
                    cur += "2";
                    break;
                case R.id.num3:
                    cur += "3";
                    break;
                case R.id.num4:
                    cur += "4";
                    break;
                case R.id.num5:
                    cur += "5";
                    break;
                case R.id.num6:
                    cur += "6";
                    break;
                case R.id.num7:
                    cur += "7";
                    break;
                case R.id.num8:
                    cur += "8";
                    break;
                case R.id.num9:
                    cur += "9";
                    break;
                default:
            }
            if (tempFocus) {
                startTv.setText(cur);
            } else {
                endTv.setText(cur);
            }
        };

        num0.setOnClickListener(numListener);
        num1.setOnClickListener(numListener);
        num2.setOnClickListener(numListener);
        num3.setOnClickListener(numListener);
        num4.setOnClickListener(numListener);
        num5.setOnClickListener(numListener);
        num6.setOnClickListener(numListener);
        num7.setOnClickListener(numListener);
        num8.setOnClickListener(numListener);
        num9.setOnClickListener(numListener);

        clear1.setOnClickListener(v -> {
            tempFocus = true;
            startCursor.setVisibility(View.VISIBLE);
            endCursor.setVisibility(View.GONE);
            startTv.setText("0");
        });
        clear2.setOnClickListener(v -> {
            tempFocus = false;
            startCursor.setVisibility(View.GONE);
            endCursor.setVisibility(View.VISIBLE);
            endTv.setText("0");
        });
        back.setOnClickListener(v -> {
            if (tempFocus) {
                String cur = startTv.getText().toString().trim();
                cur = cur.substring(0, cur.length() - 1);
                if (cur.length() == 0) {
                    cur = "0";
                }
                startTv.setText(cur);
            } else {
                String cur = endTv.getText().toString().trim();
                cur = cur.substring(0, cur.length() - 1);
                if (cur.length() == 0) {
                    cur = "0";
                }
                endTv.setText(cur);
            }
        });

        confirm.setOnClickListener(v -> {
            int start = Integer.parseInt(startTv.getText().toString());
            if (start > totalCycle || start < 1) {
                ToastUtils.get().showText("基线超出范围[ 1 , " + totalCycle + " ]，请修改！");
                return;
            }
            int stop = Integer.parseInt(endTv.getText().toString());
            if (stop > totalCycle || stop < 1) {
                ToastUtils.get().showText("基线超出范围[ 1 , " + totalCycle + " ]，请修改！");
                return;
            }
            if (stop - start < 1) {
                ToastUtils.get().showText("基线终点必须大于基线起点！");
                return;
            }
            if (listener != null) listener.onChanged(start, stop);
            BaselineDialog.this.dismiss();
        });
        return view;
    }

    public interface OnChangedListener {
        void onChanged(int start, int stop);
    }
}